package com.example.finalsystem.Controller;

import com.example.finalsystem.Entity.User;
import com.example.finalsystem.Service.ClientService;
import com.example.finalsystem.Service.LoginService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;

@RestController
public class LoginController {
    @Autowired
    LoginService loginService;

    @PostMapping("/login")
    @ResponseBody
    public String login(@RequestParam String userId,@RequestParam String userPassword,   HttpServletRequest request)
    {
        Long st = System.currentTimeMillis();
//        SqlSession sqlSession = openSession();
        String result =    loginService.userLogin(userId,userPassword);
        Long ed = System.currentTimeMillis();
        Long time = ed -st;
        System.out.println("first : "+time);
        if(result.equals("登录成功"))
        {
            st =System.currentTimeMillis();
            User user = loginService.getUserInfo(userId);
            ed = System.currentTimeMillis();
            System.out.println("second: "+(ed-st));
            request.getSession().setAttribute("user",user);
//            sqlSession.close();
            return "SUCCESS";
        }
        else {
//            sqlSession.close();
            return result;
        }
    }
    @PostMapping("/logOut")
    public String logOut(HttpServletRequest request)
    {
        request.getSession().setAttribute("user",null);
        return "SUCCESS";
    }


}
